﻿using System;
using System.Web;
using System.Web.Caching;
using CoffeeScriptHandler;

namespace CoffeeScript
{
    public class CoffeeScriptHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            Cache cache = HttpRuntime.Cache;
            string path = context.Request.PhysicalPath;

            if (cache[path] == null)
            {
                var coffeescriptCompiler = new CoffeeScriptCompiler(new ScriptReader(), new TempScriptWriter());
                cache[path] = coffeescriptCompiler.EvaluateScript(path);
            }
            context.Response.Write(cache[path]);
        }

        public bool IsReusable
        {
            get { return true; }
        }
    }
}
